#include <rtthread.h>
#include "base.h"

static struct rt_thread led_thread;
static rt_uint8_t led_stack[4096];
static struct rt_thread key_thread;
static rt_uint8_t key_stack[4096];


void led_task_entry (void * param) {
    RT_UNUSED(param);
        
    // param == RT_NULL
    while (1) {
        led_toggle(LED0);
        rt_thread_mdelay(500);
    }
}


void key_task_entry (void *param) {
    RT_UNUSED(param);
    
    int press_count = 0;
 
    while (1) {
        if (key_pressed()) {
            press_count++;
            led_set(LED1, 1);
            
            if (press_count == 3) {
                rt_thread_detach(&led_thread);
            }
        } else {
            press_count = 0;
            led_set(LED1, 0);
        }

        rt_thread_mdelay(1000);
    }
}

/* 主函数，创建线程并启动 */
int main(void)
{

    hardware_init();  // 初始化硬件基础配置
    
    rt_err_t err = rt_thread_init(
        &led_thread,
        "led_heartbeat",
        led_task_entry,
        RT_NULL,
        led_stack,
        4096,
        0,
        5
    );
    if (err == RT_EOK) rt_thread_startup(&led_thread);
    
    err = rt_thread_init(
        &key_thread,
        "key_monitor",
        key_task_entry,
        RT_NULL,
        key_stack,
        4096,
        0,
        5
    );
    if (err == RT_EOK) rt_thread_startup(&key_thread);
    return 0;
}
